package com.rqb.redisson.controller;

import com.rqb.redisson.service.RedissonService;
import com.rqb.redisson.service.RedissonUtils;
import com.rqb.redisson.service.DistributedRedisLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by baiguantao on 2017/4/17.
 */
@Controller
@RequestMapping("/redisson/")
public class RedissonController {
    @Autowired
    private RedissonService redissonService;
    @Autowired
    private RedissonUtils redissonUtils;
    //Controller级别
    @Autowired
    private DistributedRedisLock2 DistributedRedisLock2;
    //Service 层级别
    @Autowired
    private DistributedRedisLock DistributedRedisLock;
    @RequestMapping("hello")
    public String hello(){
        redissonService.GetLock();
        ExecutorService executorService= Executors.newCachedThreadPool();

        try {
            for (int i = 0; i < 1000; i++) {
                executorService.execute( new Thread(new Runnable() {
                    public void run() {
                        try {
                            String key = "test123";
                            if(DistributedRedisLock.acquireUnMutil(key)) {
                                //Thread.sleep(10); //获得锁之后可以进行相应的处理
                                System.err.println("======获得锁后进行相应的操作======");
                                //DistributedRedisLock.release(key);
                                //System.err.println("=============================");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            executorService.shutdown();
        }
        return "hello";
    }
    @RequestMapping("hello2")
    public String hello2(){
        redissonService.GetLock();
        ExecutorService executorService= Executors.newCachedThreadPool();

        try {
            for (int i = 0; i < 1000; i++) {
                executorService.execute( new Thread(new Runnable() {
                    public void run() {
                        try {
                            String key = "test1232";
                            if(DistributedRedisLock2.acquireUnMutil(key)) {
                                //Thread.sleep(10); //获得锁之后可以进行相应的处理
                                System.err.println("======获得锁后进行相应的操作======");
                                //DistributedRedisLock.release(key);
                                //System.err.println("=============================");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            executorService.shutdown();
        }
        return "hello";
    }
    @RequestMapping("hello4")
    public String hello4(){
        redissonService.GetLock();
        ExecutorService executorService= Executors.newCachedThreadPool();

        try {
            for (int i = 0; i < 1000; i++) {
                executorService.execute( new Thread(new Runnable() {
                    public void run() {
                        try {
                            String key = "test1232";
                            if(DistributedRedisLock.acquireUnMutil(key)) {
                                //Thread.sleep(10); //获得锁之后可以进行相应的处理
                                System.err.println("======获得锁后进行相应的操作======");
                                //DistributedRedisLock.release(key);
                                //System.err.println("=============================");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            executorService.shutdown();
        }
        return "hello";
    }
    @RequestMapping("hello3")
    public String hello3(){
        redissonUtils.test();
        return "hello";
    }
}
